{% extends "base.html" %}

{% block title %}用户管理 - {{ super() }}{% endblock %}

{% block content %}
<div class="container">
    <h1 class="mb-4">用户管理</h1>
    
    <div class="table-responsive">
        <table class="table table-striped table-hover">
            <thead>
                <tr>
                    <th>ID</th>
                    <th>用户名</th>
                    <th>邮箱</th>
                    <th>姓名</th>
                    <th>角色</th>
                    <th>注册时间</th>
                    <th>操作</th>
                </tr>
            </thead>
            <tbody>
                {% for user in users.items %}
                <tr>
                    <td>{{ user.id }}</td>
                    <td>{{ user.username }}</td>
                    <td>{{ user.email }}</td>
                    <td>{{ user.name or '-' }}</td>
                    <td>
                        {% if user.role == 'admin' %}
                        <span class="badge bg-danger">管理员</span>
                        {% else %}
                        <span class="badge bg-secondary">普通用户</span>
                        {% endif %}
                    </td>
                    <td>{{ user.created_at.strftime('%Y-%m-%d %H:%M') }}</td>
                    <td>
                        <div class="btn-group">
                            <a href="{{ url_for('admin.edit_user', id=user.id) }}" class="btn btn-sm btn-outline-primary">编辑</a>
                            {% if user.id != current_user.id %}
                            <button type="button" class="btn btn-sm btn-outline-danger" onclick="deleteUser({{ user.id }})">删除</button>
                            {% endif %}
                        </div>
                    </td>
                </tr>
                {% endfor %}
            </tbody>
        </table>
    </div>

    {% if users.pages > 1 %}
    <nav aria-label="Page navigation" class="mt-4">
        <ul class="pagination justify-content-center">
            {% for page_num in users.iter_pages() %}
                {% if page_num %}
                    <li class="page-item {% if page_num == users.page %}active{% endif %}">
                        <a class="page-link" href="{{ url_for('admin.users', page=page_num) }}">{{ page_num }}</a>
                    </li>
                {% else %}
                    <li class="page-item disabled"><span class="page-link">...</span></li>
                {% endif %}
            {% endfor %}
        </ul>
    </nav>
    {% endif %}
</div>

<form id="delete-form" method="post" style="display: none;">
    <input type="hidden" name="csrf_token" value="{{ csrf_token() }}">
</form>

<script>
function deleteUser(userId) {
    if (confirm('确定要删除这个用户吗？此操作不可恢复。')) {
        var form = document.getElementById('delete-form');
        form.action = "{{ url_for('admin.delete_user', id=0) }}".replace('0', userId);
        form.submit();
    }
}
</script>
{% endblock %} 